<link rel="stylesheet" href="/static/admin/bootstrap-table/bootstrap-table.css">

<div class="row wrapper border-bottom white-bg page-heading">
    <div class="col-lg-10">
        <h2>友情链接管理</h2>
        <ol class="breadcrumb">
            <li class="breadcrumb-item">
                <a href="/">首页</a>
            </li>
            <li class="breadcrumb-item">
                <a href="/admin">后台</a>
            </li>
            <li class="breadcrumb-item active">
                <strong>友情链接管理</strong>
            </li>
        </ol>
    </div>
    <div class="col-lg-2">

    </div>
</div>
<div class="wrapper wrapper-content">
    <div class="row">
        <div class="col-lg-4">
            <div class="ibox ">
                <div class="ibox-title">
                    <h5>添加链接
                        <small>在这里添加一个友情链接</small>
                    </h5>
                    <div class="ibox-tools">
                        <a class="collapse-link">
                            <i class="fa fa-chevron-up"></i>
                        </a>
                        <a class="close-link">
                            <i class="fa fa-times"></i>
                        </a>
                    </div>
                </div>
                <div class="ibox-content">
                    <div class="row">
                        <div class="col-sm-12 b-r">
                            <form role="form">
                                <div class="form-group"><label>名称</label> <input type="text"
                                                                                 placeholder="例如 GitHub"
                                                                                 class="form-control" name="name"></div>
                                <div class="form-group"><label>网址</label> <input type="text"
                                                                                 placeholder="例如 http://github.com"
                                                                                 class="form-control" name="url"></div>
                                <div class="form-group"><label for="inputPassword4">描述</label>
                                    <textarea class="form-control" rows="3" name="description"
                                              placeholder="简述这个链接的作用"></textarea></div>
                                <div>
                                    <button class="btn btn-sm btn-primary float-right m-t-n-xs" id="submit"
                                            type="button">
                                        <strong>提交</strong></button>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-lg-8">
            <div class="ibox ">
                <div class="ibox-title">
                    <h5>链接列表管理
                        <small>把没用的链接都删掉</small>
                    </h5>
                    <div class="ibox-tools">
                        <a class="collapse-link">
                            <i class="fa fa-chevron-up"></i>
                        </a>
                        <a class="close-link">
                            <i class="fa fa-times"></i>
                        </a>
                    </div>
                </div>
                <div class="ibox-content">
                    <div class="table-responsive">
                        <table id="table"></table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script src="/static/admin/bootstrap-table/bootstrap-table.js"></script>
<script src="/static/admin/layer/layer.js"></script>
<script>
    $('#table').bootstrapTable({
        url: '/api/link/list',
        responseHandler: responseHandler,
        columns: [{
            field: 'Id',
            title: 'ID'
        }, {
            field: 'Name',
            title: '名称'
        }, {
            field: 'Url',
            title: '标识'
        }, {
            field: 'Description',
            title: '描述'
        }, {
            field: 'Option',
            title: '操作',
            align: 'center',
            formatter: operateFormatter,
            events: "operateEvents",
        }],
    });

    function responseHandler(res) {
        return res.data;
    }

    function operateFormatter(value, row, index) {
        return [
            '<div class="operate-group">',
            '<a class="update layui-layer-btn0" href="#">修改</a>',
            '<a class="delete layui-layer-btn0" href="#">删除</a>',
            '</div>'
        ].join('');
    }

    // 定义layer主题名称
    layer.config({
        skin: 'layer-class'
    });
    window.operateEvents = {
        'click .update': function (e, value, row, index) {
            layer.open({
                type: 2,
                title: '修改分类信息',
                shadeClose: true,
                shade: 0.8,
                area: ['380px', '90%'],
                content: '/admin/link/update?id=' + row.Id //iframe的url
            });
        },
        'click .delete': function (e, value, row, index) {
            layer.confirm('确定删除？', function (index) {
                $.get("/api/link/delete?id=" + row.Id, function (res) {
                    if (res.Error === 0) {
                        layer.open({
                            skin: 'layer-class',
                            title: res.Title
                            , content: res.Msg,
                            yes: function (index) {
                                window.location.href = "/admin/link";
                            }
                        });
                    } else {
                        layer.msg(res.Title + res.Msg, {
                            icon: 2,
                            shade: [0.8, '#393D49'], // 透明度  颜色
                            time: 2000
                        });
                    }
                }, "json");
                layer.close(index);
            });
        }
    };
    $("#submit").click(function () {
        $(document).ready(function () {
            const postData = $("form").serialize();
            $.post("/api/link/add", postData, function (res) {
                if (res.Error === 0) {
                    layer.open({
                        title: res.Title
                        , content: res.Msg,
                        yes: function (index) {
                            window.location.href = "/admin/link";
                            layer.close(index); //如果设定了yes回调，需进行手工关闭
                        }
                    });
                } else {
                    layer.msg(res.Title + res.Msg, {
                        icon: 2,
                        shade: [0.8, '#393D49'], // 透明度  颜色
                        time: 2000
                    });
                }
            }, "json");
            return false;
        });
    });
</script>